package scoreCard.LivestockJudging3C;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;
import java.nio.channels.FileChannel;
import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.os.Environment;
import android.util.Log;

public class SaveFile extends Activity {
	
	// Save Audio
	// Need date checker, parsing in data need to remove spaces
	public boolean SaveAudio(String name, String ctype) {
		String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
		
		String path = extStorageDirectory + "/3C/Competition/" + name;
		String filename = "Recording.3gp";
		
		// Check dir for same folder
		boolean exists = (new File(path)).exists();  
		if (!exists) {
			new File(path).mkdir();
		}
		
		// Check dir for class folder
		String path2 = path + "/" + ctype;
		boolean exists2 = (new File(path2)).exists();  
		if (!exists2) {
			new File(path2).mkdir();
		}
		
		// Copy file from temp to comp folder		
		try {
			File sd = Environment.getExternalStorageDirectory();
		    if (sd.canWrite()) {
		        String source_reason= extStorageDirectory + "/3C/Temp/temp_reason.3gp";
		        String dest_reason= path2;
		        File destination= new File(dest_reason, filename);
		        FileChannel src = new FileInputStream(source_reason).getChannel();
		        FileChannel dst = new FileOutputStream(destination).getChannel();
		        dst.transferFrom(src, 0, src.size());
		        src.close();
		        dst.close();
		    } else {
		    	return false;
		    }
		} catch (Exception e) {
			// Error for unable to read/write files
		}
		
		// Delete from temp
		File bye = new File(extStorageDirectory+"/3C/Temp/temp_reason.3gp");
		bye.delete();
		
		return true;
	}
	
	// Comp name, class type, score
	public boolean SaveScore(String compName, String ctype, int score, boolean isQuestionScore, boolean createClassFolder) {
		String extStorageDirectory = Environment.getExternalStorageDirectory().toString();		
		String path = extStorageDirectory + "/3C/Competition/" + compName;		
				
		// Check dir for same folder		
		boolean exists = (new File(path)).exists();		
		if (!exists) {		
			new File(path).mkdir();		
		}
		
		// Check dir for class folder
		if (createClassFolder) {
			String path2 = path + "/" + ctype;
			boolean exists2 = (new File(path2)).exists();  
			if (!exists2) {
				new File(path2).mkdir();
			}
		}
				
		FileInputStream fis = null;
		ObjectInputStream in = null;
		Map<String, Integer> scores = null;
		try {
			fis = new FileInputStream(path + "/scores.ser");
			in = new ObjectInputStream(fis);
			scores = (Map<String, Integer>) in.readObject();
			if (scores == null) {
				scores = new HashMap<String, Integer>();
			}
			if (isQuestionScore)
			{
				scores.put(ctype + 'Q', score);
			} else
			{
				scores.put(ctype, score);
			}
		} catch (FileNotFoundException e) {
			scores = new HashMap<String, Integer>();
			scores.put(ctype, score);
			e.printStackTrace();
		} catch (StreamCorruptedException e) {
			scores = new HashMap<String, Integer>();
			scores.put(ctype, score);
			e.printStackTrace();
		} catch (IOException e) {
			scores = new HashMap<String, Integer>();
			scores.put(ctype, score);
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			scores = new HashMap<String, Integer>();
			scores.put(ctype, score);
			e.printStackTrace();
		} finally {
			try {
				if (fis != null) {
					fis.close();
				}
				if (in != null) {
					in.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		FileOutputStream fos = null;
		ObjectOutputStream out = null;
		try {
			fos = new FileOutputStream(path + "/scores.ser");
			out = new ObjectOutputStream(fos);
			out.writeObject(scores);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (fos != null) {
					fos.close();
				}
				if (out != null) {
					out.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return true;
	}
}
